﻿using AutoMapper;
using DocumentFormat.OpenXml.Drawing.Charts;
using DocumentFormat.OpenXml.EMMA;
using MediatR;
using Microsoft.AspNetCore.Connections.Features;
using Microsoft.VisualBasic;
using SmartProduction.Domain.yjh;
using SmartProduction.ErrorCode;
using SmartProduction.Infarstructrue.Interface;
using SmartProduction.Write.API.Application.Commands.ProductPlan;
using SmartProduction.Write.API.Application.Commands.ProductTwo;

namespace SmartProduction.Write.API.Application.CommandHandlers.ProductTwo
{
    /// <summary>
    /// 主生产计划添加命令处理
    /// </summary>
    public class ProductTwoCommandHandler : IRequestHandler<ProductTwoCommand, APIResult<object>>
    {
        /// <summary>
        /// 著生产计划明细仓储
        /// </summary>
        private readonly IBaseRepository<ProductPlanTwo> productRep;
        private readonly IMapper mapper;

        /// <summary>
        /// 映射
        /// </summary>

        private readonly IBaseRepository<ProductPlan> productplanrep;

        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="productRep">著生产计划明细仓储</param>
        /// <param name="mapper">映射</param>
        public ProductTwoCommandHandler(IBaseRepository<ProductPlanTwo> productRep, IMapper mapper, IBaseRepository<ProductPlan> productplanrep)
        {
            this.productRep = productRep;
            this.mapper = mapper;
            this.productplanrep = productplanrep;
        }

        public async Task<APIResult<object>> Handle(ProductTwoCommand request, CancellationToken cancellationToken)
        {
            APIResult<object> result = new APIResult<object>();


            ProductTwoCommand add=new ProductTwoCommand ();
            add.ProductPlanId= request.ProductPlanId;
            add.PlanName=request.PlanName;
            add.PlanCode=request.PlanCode;
            add.PlanDes=request.PlanDes;
            add.ApproveStatus=request.ApproveStatus;
            add.IsStatus=request.IsStatus;  
            add.MakeListPerson=request.MakeListPerson;
            add.MakeListDepart=request.MakeListDepart;

            var infro=mapper.Map<ProductPlan>(add);
            infro.CreateTime=DateTime.Now;

            var data=productplanrep.AddAsync(infro).Result;
            if (data > 0)
            {
                var code = productRep.AddRanges(request.productPlanTwos).Result;
                if (code > 0)
                {
                    result.Code = APIEnums.添加成功;
                    result.Msg = APIEnums.添加成功.ToString();
                    result.data = code;
                }
                else
                {

                    result.Code = APIEnums.添加失败;
                    result.Msg = APIEnums.添加失败.ToString();
                    result.data = code;
                }
            }
            else
            {
                result.Code = APIEnums.添加失败;
                result.Msg = APIEnums.添加失败.ToString();
                result.data = data;
            }
            return await Task.FromResult(result);

        }
    }
}

